/*
 * Grabber.cpp
 *
 *  Created on: Apr 6, 2011
 *      Author: dhargrav
 */

#include "Quotes/Grabber.h"

#include <QUrl>
#include <QDebug>

namespace Quotes {

QString Grabber::msHost("http://download.finance.yahoo.com");
QString Grabber::msPath("/d/quotes.csv?s=%1&f=sl1");

Grabber::Grabber() {
}

Grabber::~Grabber() {
}

void Grabber::getQuote(const QString &sym)
{
	mSymbol = sym;

	// Build the URL
	QUrl url(msHost + msPath.arg(sym));
	if(!url.isValid())
		emit quoteError(mSymbol);

	manager = new QNetworkAccessManager(this);
	QNetworkRequest request(url);
	currentReply = manager->get(request);

	connect(currentReply, SIGNAL(readyRead()), this, SLOT(readyRead()));
	connect(currentReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error(QNetworkReply::NetworkError)));
}

void Grabber::readyRead()
{
	int statusCode = currentReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
	if(statusCode >= 200 && statusCode < 300)
	{
		QByteArray data = currentReply->readAll();
		qDebug() << data;
	}
}

}
